package com.dragon.app.commons.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.dragon.app.ykd.service.ClientAppService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dragon.app.commons.constant.ErrorCode;
import com.dragon.app.commons.dto.BankcardSaveDTO;
import com.dragon.app.commons.utils.UserUtils;
import com.dragon.app.marvel2.NewMarvelCoreService;
import com.dragon.app.ykd.constant.ApplyNode;
import com.dragon.app.ykd.dto.AcLoanApplyNode;
import com.dragon.app.ykd.dto.BankCardInfosDTO;
import com.dragon.app.ykd.dto.BankCardLoanDTO;
import com.dragon.app.ykd.service.AcLoanApplyNodeService;
import com.dragon.common.constant.LoanStatusConstant;
import com.dragon.loan.entity.AcLoanApplyInfo;
import com.dragon.loan.service.LoanInfoIDragonService;
import com.fintech.common.entity.ResultData;
import com.fintech.common.enums.ReturnCode;
import com.fintech.common.vo.FintechResult;
import com.fintech.third.service.sms.SmsService;
import com.fintech.user.entity.RealNameAuthentication;
import com.fintech.user.entity.User;
import com.fintech.user.service.RealNameAuthenticationService;
import com.marvels.approve.loan.api.LoanApplyProvideApi;
import com.marvels.common.dto.pay.MarvelsPayResultDTO;
import com.marvels.common.dto.pay.McfePaymentUserBindCard;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.financial.loan.dto.McfeSustainBankListDTO;
import com.marvels.financial.loan.service.BankLimitRelevantApi;
import com.marvels.financial.pay.service.PayService;

/**
 * 银行或者关于银行卡
 * @author ljs
 *
 */
@Service("bankAppService")
public class BankServiceImpl implements BankService{
	@Autowired
	private LoanInfoIDragonService loanInfoIDragonService;
	@Autowired
	private RealNameAuthenticationService realNameAuthenticationService;
	@Autowired
	private NewMarvelCoreService newMarvelCoreService;
	@Autowired
	private AcLoanApplyNodeService acLoanApplyNodeService;
	@Autowired
	private LoanApplyProvideApi loanApplyProvideApi;
	@Autowired
	private PayService payService;
	@Autowired
	private BankLimitRelevantApi bankLimitRelevantApi;
	@Autowired
	private ClientAppService clientAppService;
	@Autowired
	private SmsService smsService;

	@Value("${config.properties.path}")
	private  String configPropertiesPath;

	/**
	 * 根据用户Id按照类型获取银行卡的信息<br>
	 * 0-放款卡,1-还款卡,2-信用卡<br>
	 *
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@Override
	public FintechResult getBankCardList(String accessToken, String idFintechUmUser,
			BankCardInfosDTO bankCardInfosDTO) {
		List<Map<String, Object>> listAcUserBankByUserId = null;
		List<Map<String, Object>> returnList = new ArrayList<>();
		try {
			//1 获取用户信息以及实名认证信息
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());

			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_QUERYBINDEDLIST, reqMap);
			*/
			String idno = realNameAuthentication.getIdentityCardNo();
			String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.queryBindedSuccessList(idno, paymentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());

			MarvelsLogUtil.getInstance().info("========getBankCardList:" + JSON.toJSONString(jsonResult));
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "获取卡列表失败");
			}
			List<McfePaymentUserBindCard> rtnMap =  (List<McfePaymentUserBindCard>) jsonResult.getData();
			for (McfePaymentUserBindCard map : rtnMap) {
				Map<String, Object> bankMap = new HashMap<>();
				bankMap.put("id", map.getId());
				bankMap.put("bankCode", map.getBankCode());
				bankMap.put("bankName", map.getBankName());
				bankMap.put("mobileNum", map.getPhone());
				bankMap.put("bankCardNo", map.getBankCardNo());
				bankMap.put("defaultCard", map.getDefaultCard());
				bankMap.put("bankCardType", "借记卡");
				returnList.add(bankMap);
			}
			return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", returnList);

		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====bankCard/infos=====", e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}


	/**
	 * 根据用户Id按照类型获取绑定银行卡信息<br>
	 * 0-放款卡,1-还款卡,2-信用卡<br>
	 *
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@Override
	public FintechResult getBankBindList(String accessToken, String idFintechUmUser,
			BankCardInfosDTO bankCardInfosDTO) {
		List<Map<String, Object>> listAcUserBankByUserId = null;
		try {
			//1 获取用户信息以及实名认证信息
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());

			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_BANKBINDLIST, reqMap);*/

			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			MarvelsPayResultDTO data = payService.bankBindList(reqMap);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());


			MarvelsLogUtil.getInstance().info("========getBankCardList:" + JSON.toJSONString(jsonResult));
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "获取卡列表失败");
			}
			Map<String, Object> rtnMap =  (Map<String, Object>) jsonResult.getData();
			Map<String, Object> bankMap = new HashMap<>();
			bankMap.put("isBind", rtnMap.get("isBind"));

			List<McfePaymentUserBindCard> bindCard =  (List<McfePaymentUserBindCard>) rtnMap.get("bindCard");
			List<Map<String, Object>> resultList =new ArrayList<Map<String, Object>>();
			if(CollectionUtils.isNotEmpty(bindCard)) {
				bindCard.forEach(paymentUserBindCard -> {
					Map<String, Object> map =new HashMap<String, Object>();
					map.put("id", paymentUserBindCard.getId());
					map.put("bankCode", paymentUserBindCard.getBankCode());
					map.put("bankName", paymentUserBindCard.getBankName());
					map.put("mobileNum", paymentUserBindCard.getPhone());
					map.put("bankCardNo", paymentUserBindCard.getBankCardNo());
					map.put("bankCardType", "借记卡");
					resultList.add(map);
				});
			}
			bankMap.put("bindCards", resultList);
			return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", bankMap);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====bankCard/infos=====", e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}

	/**
	 * 接收用户填写的的宝付发送给客户的验证
	 * 去宝付那边获取这个卡跟这个商户号对应的协议号
	 * @param bankcardSaveDTO
	 * @param accessToken
	 * @param idFintechUmUser
	 * @return
	 * @author shan
	 */
	@Override
	public FintechResult confrimBindBankCard(BankcardSaveDTO bankcardSaveDTO,
			 String accessToken,
			 String idFintechUmUser) {
		UUID randomUUID = UUID.randomUUID();
		String uuid = randomUUID.toString();

		MarvelsLogUtil.getInstance().info("-----------bankCard/save------s--------" + uuid + "----" + bankcardSaveDTO.toString());
		try {
			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("requestNo", bankcardSaveDTO.getRequestNo());
			reqMap.put("validateCode", bankcardSaveDTO.getValidCode());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_BINDCARD, reqMap);*/

			String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.bindCard(bankcardSaveDTO.getRequestNo(), bankcardSaveDTO.getValidCode(), paymentChannel,bankcardSaveDTO.getMobileNum());
			FintechResult jsonResult = new FintechResult();
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setData(data.getData());
			jsonResult.setMessage(data.getMessage());


			MarvelsLogUtil.getInstance().info("========saveBankCardNew:" + JSON.toJSONString(jsonResult));
			// 已经绑定的卡不能再绑定
			if ("-10000001".equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "您的卡[" + bankcardSaveDTO.getBankCardNum() + "]已绑定，不能重复绑定");
			}
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "绑卡异常");
			}
			String status = (String) jsonResult.getData();
			// 成功状态
			if ("BIND_SUCCESS".equals(status)) {
				return FintechResult.getResult(ErrorCode.SUCCESS, "绑卡成功", null);
			}
			return FintechResult.getFailResult(ErrorCode.FAIL, "绑卡失败");

		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("-----------bankCard/save------error-------" + uuid + "-------" + bankcardSaveDTO.toString(),
						e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}

	/**
	 * 设置放款卡 还款卡 type 1 还款卡 2 放款卡 id 放款卡的id
	 * 线上绑卡
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@Override
	public FintechResult bankCardLoan(String loanId, BankCardLoanDTO bankCardLoanDTO,
			String accessToken, String idFintechUmUser) {

		MarvelsLogUtil.getInstance().info("-----------bankCard/loan/{loanId}------s------------" + JSON.toJSONString(bankCardLoanDTO)+ "--loanId = " + loanId);
		AcLoanApplyInfo loan = new AcLoanApplyInfo();
		try {

			//1 检查
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}

			//1.1 是否存在进件信息
			AcLoanApplyInfo acLoanApplyInfo = loanInfoIDragonService.queryLoanBasicInfo(loanId);
			if(acLoanApplyInfo == null){
				return FintechResult.getResult(ErrorCode.EXCEPTION, "未查询到进件信息", null);
			}
			//2 设置卡操作
			if(org.apache.commons.lang3.StringUtils.isBlank(bankCardLoanDTO.getReceiveAcct())
					|| org.apache.commons.lang3.StringUtils.isBlank(bankCardLoanDTO.getRepayAcct())){
				return FintechResult.getFailResult(ReturnCode.fail.getErrorCode(), "卡号信息不存在");
			}

			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			String currentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			// 设置默认卡
			//MarvelsPayResultDTO data = payService.updateDefaultCard(realNameAuthentication.getIdentityCardNo(), currentChannel, loanId, bankCardLoanDTO.getBankCardNo());
			MarvelsPayResultDTO data = payService.updateDefaultCard(realNameAuthentication.getIdentityCardNo(), currentChannel, loanId, bankCardLoanDTO);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());

			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, data.getMessage());
			}

			if (LoanStatusConstant.LOAN_STATUS_K00.equals(acLoanApplyInfo.getStatus())) {
				loanInfoIDragonService.updateLoanStatus(loanId, LoanStatusConstant.LOAN_STATUS_K01);
			}

			// 扭转流程节点到绑卡完成
			AcLoanApplyNode applyNode = acLoanApplyNodeService.queryLastApplyNode(loanId);
			if (applyNode == null) {
				return FintechResult.getFailResult(ReturnCode.fail.getErrorCode(), "查询最新的节点信息为空");
			}
			// 当前节点为进件初始化节点时，才扭转节点状态到绑卡完成
			if (applyNode.getNodeCode() == ApplyNode.APPLY_INIT.getCode()) {
				acLoanApplyNodeService.insertSelective(loanId, ApplyNode.BIND_CARD);
			}

		   MarvelsLogUtil.getInstance().info("-----------bankCard/loan/{loanId}------e------------" + bankCardLoanDTO+ "--loanId = " + loanId);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("-----------bankCard/loan/{loanId}------error-------" + bankCardLoanDTO + "--loanId = " + loanId, e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
		return clientAppService.doApprove(loanId);
		//return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", null);
	}

	/**
	 * 根据银行产品信息预绑卡接口发送验证码
	 *
	 * @param bankcardSaveDTO
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public FintechResult bankPreBindCard(BankcardSaveDTO bankcardSaveDTO, String accessToken, String idFintechUmUser) {
		String loanId = null;
		try {
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			if(realNameAuthentication == null) 	{return FintechResult.getResult(ErrorCode.EXCEPTION, "未实名", "");}


			//查询是否支持银行
			/*Map<String, String> reqMap = new HashMap<>();
			reqMap.put("bankCode", bankcardSaveDTO.getBankCode());
			reqMap.put("bankName", bankcardSaveDTO.getBankName());
			// 预绑卡调用新接口
			reqMap.put("bankCardNo", bankcardSaveDTO.getBankCardNum());
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			reqMap.put("userName", realNameAuthentication.getRealName());
			reqMap.put("phone", bankcardSaveDTO.getMobileNum());
			FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_PREBINDCARD, reqMap);*/

			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("bankCode", bankcardSaveDTO.getBankCode());
			reqMap.put("bankName", bankcardSaveDTO.getBankName());
			// 预绑卡调用新接口
			reqMap.put("bankCardNo", bankcardSaveDTO.getBankCardNum());
			reqMap.put("idno", realNameAuthentication.getIdentityCardNo());
			reqMap.put("userName", realNameAuthentication.getRealName());
			reqMap.put("phone", bankcardSaveDTO.getMobileNum());
			String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.preBind(reqMap, paymentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());

			MarvelsLogUtil.getInstance().info("========bankPreBindCard:" + JSON.toJSONString(jsonResult));
			// 已经绑定的卡不能再绑定
			if ("-10000001".equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "您的卡[" + bankcardSaveDTO.getBankCardNum() + "]已绑定，不能重复绑定");
			}
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "预绑卡异常");
			}
			Map<String, String> rtnMap = (Map<String, String>) jsonResult.getData();
			// 待验证状态
			if ("TO_VALIDATE".equals(rtnMap.get("returnStatus")) || "BIND_SUCCESS".equals(rtnMap.get("returnStatus"))) {
				return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", rtnMap);
			}
			return FintechResult.getFailResult(ErrorCode.FAIL, "预绑卡失败");

		}catch(Exception e) {
			MarvelsLogUtil.getInstance().error("预绑卡异常=========================loanId" + loanId, e);
			return FintechResult.getResult(ErrorCode.EXCEPTION, "绑卡失败, 请换卡尝试", "");
		}
	}

	@Override
	public FintechResult sustainBankList(String accessToken,String idFintechUmUser) {
		User user = UserUtils.getUser(accessToken, idFintechUmUser);
		if (user == null) {
			return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
		}
		try {
			List<McfeSustainBankListDTO> list = bankLimitRelevantApi.selectSustainBankList();
			FintechResult bankResult = new FintechResult();
			bankResult.setData(list);
			bankResult.setErrorCode(ErrorCode.SUCCESS);
			bankResult.setMessage("成功");

			if (!ErrorCode.SUCCESS.equals(bankResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "查询支持银行名称列表异常");
			}else{
				return FintechResult.getResult(ErrorCode.SUCCESS,"查询支持银行列表成功",(List<?>) bankResult.getData());
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>>查询支持银行名称列表异常",e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}

	}

	@Override
	public FintechResult queryBnakCardBin(String bankCardNo) {
		MarvelsLogUtil.getInstance().info("========queryBnakCardBin========,bankCardNo=[" + bankCardNo + "]");


		/*Map<String, String> map = new HashMap<>();
		map.put("bankCardNo", bankCardNo);
		FintechResult jsonResult = newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PAY_API_QUERY_CARD_BIN, map);*/

		if(StringUtils.isEmpty(bankCardNo)){
			return FintechResult.getFailNullParam("绑定卡信息入参为空");
		}
		String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
		MarvelsPayResultDTO data = payService.getBankCardInfo(bankCardNo,paymentChannel);
		FintechResult jsonResult = new FintechResult();
		jsonResult.setMessage(data.getMessage());
		jsonResult.setErrorCode(data.getCode());
		jsonResult.setData(data.getData());

		MarvelsLogUtil.getInstance().info("========queryBnakCardBin:" + JSON.toJSONString(jsonResult));
		if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
			return FintechResult.getFailResult(ErrorCode.FAIL, "获取卡BIN信息异常");
		}
		return FintechResult.getSuccessResult(jsonResult.getData());
	}


	/**
	 * 查询绑定成功的银行卡列表
	 * 优先展示当前渠道的银行卡，如果存在老渠道的也会查出，根据is_support字段区分当前渠道是否支持
	 */
	@Override
	public FintechResult queryBankList(String accessToken, String idFintechUmUser) {
		try {
			//1 获取用户信息以及实名认证信息
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());

			String currentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.queryBankList(realNameAuthentication.getIdentityCardNo(), currentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());

			MarvelsLogUtil.getInstance().info("========queryBankList:" + JSON.toJSONString(jsonResult));
			if (!ErrorCode.SUCCESS.equals(jsonResult.getErrorCode())) {
				return FintechResult.getFailResult(ErrorCode.FAIL, "获取银行卡列表失败");
			}
			List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
			List<McfePaymentUserBindCard> rtnMap =  (List<McfePaymentUserBindCard>) jsonResult.getData();
			for (McfePaymentUserBindCard card : rtnMap) {
				Map<String, Object> bankMap = new HashMap<>();
				bankMap.put("id", card.getId());
				bankMap.put("acctName", card.getCustName());
				bankMap.put("bankCode", card.getBankCode());
				bankMap.put("bankName", card.getBankName());
				bankMap.put("mobileNum", card.getPhone());
				bankMap.put("bankCardNo", card.getBankCardNo());
				bankMap.put("defaultCard", card.getDefaultCard());
				bankMap.put("isSupport", card.getIsSupport());
				bankMap.put("bankCardType", "借记卡");
				returnList.add(bankMap);
			}
			return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", returnList);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====queryBankList=====", e);
			return FintechResult.getResult(ErrorCode.FAIL, "系统异常", null);
		}
	}


	/**
	 * 设置默认卡
	 */
	@Override
	public FintechResult setDefaultCard(String accessToken, String idFintechUmUser, BankCardLoanDTO bankCardLoanDTO) {
		//1 获取用户信息以及实名认证信息
		try {
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			ResultData<Boolean> selectByUserIdResponse = realNameAuthenticationService.selectByUserId(user.getId());
			if (!selectByUserIdResponse.getData()) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			String currentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			// 查询在途的loanId
			String loanId = loanInfoIDragonService.selectCustApplyingLoanId(realNameAuthentication.getIdentityCardNo());
			// 设置默认卡
			MarvelsPayResultDTO data = payService.updateDefaultCard(realNameAuthentication.getIdentityCardNo(), currentChannel, loanId, bankCardLoanDTO);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setData(data.getData());
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setMessage(data.getMessage());
			return jsonResult;
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error(">>>>>>>>>设置默认卡失败", e);
			return FintechResult.getResult(ErrorCode.FAIL, e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=====设置默认卡异常=====", e);
			return FintechResult.getResult(ErrorCode.EXCEPTION, "设置默认卡异常", null);
		}

	}


	@Override
	public FintechResult unbind(String bindCardNo, String accessToken, String idFintechUmUser) {
		    MarvelsLogUtil.getInstance().info("-----------unbind--start bindCardNo="+bindCardNo);
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			if(null == realNameAuthentication) {
				return FintechResult.getResult(ErrorCode.NO_VERIFY, "", null);
			}
			String paymentChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
			MarvelsPayResultDTO data = payService.unbind(realNameAuthentication.getIdentityCardNo(), bindCardNo, paymentChannel);
			FintechResult jsonResult = new FintechResult();
			jsonResult.setErrorCode(data.getCode());
			jsonResult.setData(data.getData());
			jsonResult.setMessage(data.getMessage());
			MarvelsLogUtil.getInstance().info("========unbind:" + JSON.toJSONString(jsonResult));
			return jsonResult;
	}


	/* (non-Javadoc)
	 * @see com.dragon.app.commons.service.BankService#payResultCallback(java.util.Map, java.lang.String)
	 */
	@Override
	public FintechResult payResultCallback(Map<String, String> param, String payChannel) {
		MarvelsLogUtil.getInstance().info("支付结果回调，参数="+JSON.toJSONString(param)+"，支付渠道="+payChannel);
		String str = payService.payResultCallback(param,payChannel);
		FintechResult jsonResult = new FintechResult();
		jsonResult.setMessage("成功");
		MarvelsLogUtil.getInstance().info("支付结果回调返回:" + str);
		return jsonResult;
	}
}
